﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;

namespace HYRElectric.LightningCalculation
{
	public partial class CorrectionCoefficientForm : System.Windows.Forms.Form
	{
		public CorrectionCoefficientForm()
		{
			this.InitializeComponent();
		}

		public string GetK
		{
			get
			{
				return this.tbK.Text;
			}
		}

		private void CorrectionCoefficientForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.LoadDlgData();
		}

		private void CorrectionCoefficientForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (base.DialogResult == DialogResult.OK)
			{
				this.SaveDlgData();
			}
		}

		private void SaveDlgData()
		{
			string workingLocation = Product.WorkingLocation;
			string xmlPath = Path.Combine(workingLocation, "CorrectionCoefficientForm.xml");
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(workingLocation, "Root");
			xmlHelper.SelectNode("Root\\Datas");
			xmlHelper.SetValue("xPos", base.Location.X.ToString());
			xmlHelper.SetValue("yPos", base.Location.Y.ToString());
			xmlHelper.SetValue("rdbGeneralBuilding", this.rdbGeneralBuilding.Checked.ToString());
			xmlHelper.SetValue("rdbIsolatedBuilding", this.rdbIsolatedBuilding.Checked.ToString());
			xmlHelper.SetValue("rdbMetalRoofWoodenBuilding", this.rdbMetalRoofWoodenBuilding.Checked.ToString());
			xmlHelper.SetValue("rdbSpecialBuilding", this.rdbSpecialBuilding.Checked.ToString());
			xmlHelper.SaveXml(xmlPath);
		}

		private void LoadDlgData()
		{
			string text = Path.Combine(Product.WorkingLocation, "CorrectionCoefficientForm.xml");
			if (!File.Exists(text))
			{
				this.rdbGeneralBuilding.Checked = true;
				return;
			}
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(text, "Root");
			xmlHelper.SelectNode("Root\\Datas");
			int x = Convert.ToInt32(xmlHelper.GetValue("xPos"));
			int y = Convert.ToInt32(xmlHelper.GetValue("yPos"));
			base.Location = new System.Drawing.Point(x, y);
			if (xmlHelper.GetValue("rdbGeneralBuilding") == "True")
			{
				this.rdbGeneralBuilding.Checked = true;
			}
			else
			{
				this.rdbGeneralBuilding.Checked = false;
			}
			if (xmlHelper.GetValue("rdbIsolatedBuilding") == "True")
			{
				this.rdbIsolatedBuilding.Checked = true;
			}
			else
			{
				this.rdbIsolatedBuilding.Checked = false;
			}
			if (xmlHelper.GetValue("rdbMetalRoofWoodenBuilding") == "True")
			{
				this.rdbMetalRoofWoodenBuilding.Checked = true;
			}
			else
			{
				this.rdbMetalRoofWoodenBuilding.Checked = false;
			}
			if (xmlHelper.GetValue("rdbSpecialBuilding") == "True")
			{
				this.rdbSpecialBuilding.Checked = true;
				return;
			}
			this.rdbSpecialBuilding.Checked = false;
		}

		private void rdbGeneralBuilding_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdbGeneralBuilding.Checked)
			{
				this.tbK.Text = "1";
			}
		}

		private void rdbIsolatedBuilding_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdbIsolatedBuilding.Checked)
			{
				this.tbK.Text = "2";
			}
		}

		private void rdbMetalRoofWoodenBuilding_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdbMetalRoofWoodenBuilding.Checked)
			{
				this.tbK.Text = "1.7";
			}
		}

		private void rdbSpecialBuilding_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdbSpecialBuilding.Checked)
			{
				this.tbK.Text = "1.5";
			}
		}
	}
}
